package com.xkh.common.guodong;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xkh.common.CommonApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.client.RestTemplate;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

/**
 * @author xkh
 * @date 2022/8/20 17:18
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = CommonApplication.class)
public class guodong {

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void test() {
        Integer categoryId = 70; //天神
//        Integer categoryId = 18; //琵琶
//        Integer categoryId = 25; //飞天
//        Integer categoryId = 96; //飞兔
//        Integer categoryId = 75; //大禹
        String url = "https://gd.guodongnft.com/api/nft.bazaar/index?page=1&limit=20&category_id="+0+"&keywork=果冻球&state=1&orderBy=price&sort=asc";
        HttpHeaders headers = new HttpHeaders();
        HttpEntity<String> req = new HttpEntity<>("", headers);
        ResponseEntity<JSONObject> rest = restTemplate.exchange(url, HttpMethod.GET, req,JSONObject.class);
//        System.out.println(rest);
        JSONArray jsonArray = rest.getBody().getJSONObject("data").getJSONArray("data");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        System.out.println("查询时间:" +sdf.format(new Date()));
        System.out.println("市场共寄售:"+jsonArray.size()+"个"+"\n");
        int b = 0 ;
        int c = 0;
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            JSONObject jsonName = jsonObject.getJSONObject("author");
            JSONObject condition = jsonObject.getJSONObject("condition");
            System.out.println("用户ID:"+condition.get("user_id"));
            System.out.println("用户昵称:"+jsonName.get("name"));
            System.out.println("编号:"+jsonObject.get("stock"));
            System.out.println("价格:"+jsonObject.get("price")+"\n");
            if(jsonName.get("name").equals("果冻王")){

                c ++;
            }

            if(jsonObject.getDouble("price") < 20){
                b ++;
            }
        }
        System.out.println("市场共寄售:"+b+"个"+"\n");
        System.out.println("市场共寄售:"+c+"个"+"\n");
    }

    /**
     * 果冻天神市场监控
     */
    @Test
    public void test1(){
//        Integer categoryId = 70; //天神
        Integer categoryId = 18; //琵琶
//        Integer categoryId = 25; //飞天
//        Integer categoryId = 96; //飞兔
//        Integer categoryId = 75; //大禹
        String url = "https://gd.guodongnft.com/api/nft.bazaar/index?page=1&limit=20&category_id="+categoryId+"&keywork=&orderBy=price&sort=desc";
        HttpHeaders headers = new HttpHeaders();
        HttpEntity<String> req = new HttpEntity<>("", headers);
        ResponseEntity<JSONObject> rest = restTemplate.exchange(url, HttpMethod.GET, req,JSONObject.class);
//        System.out.println(rest);
        JSONArray jsonArray = rest.getBody().getJSONObject("data").getJSONArray("data");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        System.out.println("查询时间:" +sdf.format(new Date()));
        System.out.println("市场共寄售:"+jsonArray.size()+"个"+"\n");
        int b = 0 ;
        int c = 0;
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            JSONObject jsonName = jsonObject.getJSONObject("author");
            JSONObject condition = jsonObject.getJSONObject("condition");
            System.out.println("用户ID:"+condition.get("user_id"));
            System.out.println("用户昵称:"+jsonName.get("name"));
            System.out.println("编号:"+jsonObject.get("stock"));
            System.out.println("价格:"+jsonObject.get("price")+"\n");
            if(jsonName.get("name").equals("果冻王")){

                c ++;
            }

            if(jsonObject.getDouble("price") < 20){
                b ++;
            }
        }
        System.out.println("市场共寄售:"+b+"个"+"\n");
        System.out.println("市场共寄售:"+c+"个"+"\n");
    }
}
